import requests

keysinger='周杰伦'

page = 1


def get_music(keysinger, page):
    res = requests.post(
        url="https://zz123.com/ajax/",
        data={
            "act": "search",
            "key": keysinger,  # 歌手名
            "lang": "",
            "page": page,  # 页码
        }
    )

    res_dict = res.json()
    data_list = res_dict['data']
    mp3_name, singer_name, res = music_download(data_list)
    return mp3_name, singer_name, res


def music_download(data_list):
    mp3_url = 'https://zz123.com/ajax/'
    mp3_id=[]
    mp3_name=[]
    singer_name=[]
    for data in data_list:

        mp3_id += data['id']
        mp3_name += data['mname']
        singer_name += data['sname']
        url = requests.post(mp3_url, data={
            'act': 'songinfo',
            'id': mp3_id,
        }).json()['data']['mp3']
        res = requests.get(url=url).content

    return mp3_name, singer_name, res,mp3_id


#     if keysinger in singer_name or keysinger in mp3_name:
#         with open(f'{mp3_name}.mp3', 'wb') as f:
#                     f.write(res)
#         print(f'正在下载{singer_name}的{mp3_name}')
#     else:
#         print('换人了')
#
# print('下载完成,歌曲不够将页码参数修改为2')


if __name__ == '__main__':
    mp3_name, singer_name, res=get_music(keysinger, page)
    print(mp3_name)
